package com.olt.controller.RoleController;

import com.olt.domain.user.OltRole;
import com.olt.result.ErrorEnum;
import com.olt.result.PageResult;
import com.olt.result.Result;
import com.olt.server.dto.PageInput;
import com.olt.server.role.RoleServer;
import com.olt.server.role.dto.CreateRoleInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import util.PageUtil;

@RestController
public class RoleController {
    @Autowired
    RoleServer roleServer;
    @PostMapping(value = "api/role/createRole")
    Result<String> createRole(@RequestBody CreateRoleInput input){
        OltRole role=roleServer.getOltRoleByName(input.roleNameEn);
        Result<String> result=new Result<>();
        if(role!=null&& !input.id.equals(role.getRoleId())){
            result.setError(ErrorEnum.MyError("重复的名字"));
            return result;
        }
        roleServer.createRole(input);
        result.setError(ErrorEnum.Success_Http);
        return result;
    }
    @PostMapping(value = "api/role/getRoles")
    PageResult<OltRole> getRoles(@RequestBody PageInput input){
        PageResult<OltRole> res=new PageResult<>();
        Page<OltRole> page=roleServer.getOltRole(input);
        PageUtil.setPageRes(res,page);
        res.setError(ErrorEnum.Success_Http);
        return res;
    }
    @DeleteMapping("api/role/deleteRole")
    public  Result<String> deleteRole(@Param("ids") Long[] ids){
        Result<String> res=new Result<>();
        roleServer.deleteRole(ids);
        res.setError(ErrorEnum.Success_Http);
        return res;
    }
    @GetMapping("api/role/getRoleForEdit")
    public  Result<CreateRoleInput> getRoleForEdit(@Param("id")Long id){
        Result<CreateRoleInput> result=new Result<>();
        result.setValue(roleServer.getRoleForEdit(id));
        result.setError(ErrorEnum.Success_Http);
        return result;

    }
}
